<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2017/12/29
 * Time: 下午5:28
 * 策略操作
 */
namespace Home\Controller;

use Think\Controller;

class StrategyController extends Controller
{
    public function listAll()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            $s = D('Strategy');
            $flag = $s->getAll($uid);
            if ($flag != -1) {
                $rst['status'] = true;
                $rst['info'] = '查询成功';
                $rst['data'] = $flag;
            } else {
                $rst['status'] = false;
                $rst['info'] = '查询失败';
                $rst['flag'] = C('COMMON_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }


    public function add()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            if (isset($_POST['name']) && isset($_POST['code'])) {
                $uid = intval($_SESSION['user_info']['id']);
                $name = $_POST['name'];
                $code = $_POST['code'];
                $s = D('Strategy');
                $flag = $s->addStrategy($uid, $name, $code, date('Y-m-d H:i:s', time()));
                if ($flag != -1) {
                    $rst['status'] = true;
                    $rst['info'] = '添加成功';
                } else {
                    $rst['status'] = false;
                    $rst['info'] = '添加失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '缺少参数';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }

        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');

        }
        echo json_encode($rst);
    }

    public function del()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            if (isset($_POST['id'])) {
                $pid = intval($_POST['id']);
                $s = D('Strategy');
                $flag = $s->delStrategy($uid, $pid);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '删除成功';
                } else {
                    $rst['status'] = false;
                    $rst['info'] = '删除失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '缺少参数';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }

        echo json_encode($rst);
    }

    public function update()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['code'])) {
                $pid = intval($_POST['id']);
                $name = $_POST['name'];
                $code = $_POST['code'];
                $s = D('Strategy');
                $flag = $s->updateStrategy($pid, $uid, $name, $code, date('Y-m-d H:i:s', time()));
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '更新成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '更新失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '缺少参数';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }

        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');

        }

        echo json_encode($rst);
    }


}